package com.ruoyi.sgs.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.sgs.domain.SgsHero;
import com.ruoyi.sgs.mapper.SgsHeroMapper;
import com.ruoyi.sgs.service.ISgsHeroService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * 武将Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
public class SgsHeroServiceImpl implements ISgsHeroService
{
    private static final Logger log = LoggerFactory.getLogger(SgsHeroServiceImpl.class);
    @Autowired
    private SgsHeroMapper sgsHeroMapper;

    /**
     * 查询武将
     * 
     * @param id 武将主键
     * @return 武将
     */
    @Override
    public SgsHero selectSgsHeroById(Long id)
    {
        return sgsHeroMapper.selectSgsHeroById(id);
    }

    /**
     * 查询武将列表
     * 
     * @param sgsHero 武将
     * @return 武将
     */
    @Override
    public List<SgsHero> selectSgsHeroList(SgsHero sgsHero)
    {
        return sgsHeroMapper.selectSgsHeroList(sgsHero);
    }

    /**
     * 新增武将
     * 
     * @param sgsHero 武将
     * @return 结果
     */
    @Override
    public int insertSgsHero(SgsHero sgsHero)
    {
        sgsHero.setCreateTime(DateUtils.getNowDate());
        return sgsHeroMapper.insertSgsHero(sgsHero);
    }

    /**
     * 修改武将
     * 
     * @param sgsHero 武将
     * @return 结果
     */
    @Override
    public int updateSgsHero(SgsHero sgsHero)
    {
        sgsHero.setUpdateTime(DateUtils.getNowDate());
        return sgsHeroMapper.updateSgsHero(sgsHero);
    }

    /**
     * 批量删除武将
     * 
     * @param ids 需要删除的武将主键
     * @return 结果
     */
    @Override
    public int deleteSgsHeroByIds(Long[] ids)
    {
        return sgsHeroMapper.deleteSgsHeroByIds(ids);
    }

    /**
     * 删除武将信息
     * 
     * @param id 武将主键
     * @return 结果
     */
    @Override
    public int deleteSgsHeroById(Long id)
    {
        return sgsHeroMapper.deleteSgsHeroById(id);
    }

    @Override
    public String importHeros(List<SgsHero> heroes, boolean updateSupport) {

        if (StringUtils.isNull(heroes) || heroes.size() == 0)
        {
            throw new ServiceException("导入武将数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SgsHero hero : heroes)
        {
            try
            {
                // 验证是否存在这个用户
                SgsHero u = sgsHeroMapper.selectSgsHeroByHero(hero.getHero());
                if (StringUtils.isNull(u))
                {
                    sgsHeroMapper.insertSgsHero(hero);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、武将 " + hero.getHero() + " 导入成功");
                }
                else if (updateSupport)
                {
                    if(hero.getPrice()!=null){
                        u.setPrice(hero.getPrice());
                    }
                    if (hero.getCloPrice()!=null){
                        u.setCloPrice(hero.getCloPrice());
                    }
                    if(StringUtils.isNoneBlank(hero.getArea())){
                        u.setArea(hero.getArea());
                    }
                    sgsHeroMapper.updateSgsHero(u);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、武将 " + hero.getHero() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、武将 " + hero.getHero() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、武将 " + hero.getHero() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String heroesPrice(Map<String, String> map) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<html>");
        if (StringUtils.isNotBlank(map.get("heroes"))){
            List<SgsHero> sgsHeroes = this.selectSgsHeroList(new SgsHero());
            String[] heroes = map.get("heroes").split("/");
            BigDecimal bigDecimal = new BigDecimal(0);
            for (String hero : heroes) {
                for (SgsHero sgsHero : sgsHeroes) {
                    if (sgsHero.getHero().equals(hero)){
                        stringBuilder.append(sgsHero.getHero()+"："+sgsHero.getPrice()+"<br>");
                        bigDecimal = bigDecimal.add(sgsHero.getPrice());
                    }
                }
            }
            stringBuilder.append("总价："+bigDecimal);
            stringBuilder.append("</html>");
            return stringBuilder.toString();
        }
        return null;
    }
}
